#!/bin/sh
#description:wanglei shell common functions 


#################################################
#
#	write to standard output and a file=$LOGFILE
#	need to setenv $LOGFILE

echo_tee()
{
	echo $1 | tee -a $LOGFILE
}

echo_red()
{
	echo  -e "\033[1;31;40m$1\033[0m";
}

echo_green()
{
	echo  -e "\033[1;32;40m$1\033[0m";
}


################################################
#
#	install perl_modules
#	install_perl_modules perl_modules_list

install_perl_modules()
{
	for module in $1
	do
		cd $module
		if perl Makefile.PL && make && make install 
		then
			echo_tee "install $module      [ok]"
		else
			echo_tee "install $module      [failed]"
			return 1
		fi
	done
	return 0
}


################################################
#
#	install perl 
#	install_perl perl_pkg_path 

install_perl()
{
	cd $1
	if sh Configure -de 
	then
		if make && make install
		then
			if [ -f /usr/bin/perl ] 
			then
				mv /usr/bin/perl /usr/bin/perl.bak
			fi
			ln -s /usr/local/bin/perl /usr/bin/perl
			echo_tee "install perl      [ok]"
		else
			echo_tee "install perl      [failed]"
			return 1
		fi
	else
		echo_tee "install perl/configure -de[failed]"
		return 1
	fi

	return 0
}

################################################
#
#	set env_variable
#	set_env user/sys variable value

set_env()
{
	local id=$1
	local variable=$2
	local value=$3

	sys_profile=/etc/profile
	user_profile=$HOME/.bash_profile

	if [ "$id" = "user" ]
	then
		profile=$user_profile
	else

		profile=$sys_profile
	fi

	new_profile=${profile}_new
	old_profile=${profile}_old

	if [ "$variable" = "PATH" ]
	then
		set_env_like_path $variable $value
		[ $? -ne 0 ] || return 1
		return 0
	fi


	grep -E "^\s*export $variable\>" $profile
	if [ $? -eq 0 ]
	then
		grep -E "^\s*export $variable=$value\>" $profile
		if [ $? -eq 0 ]
		then
			echo_tee "$variable=$value has been set in the $profile"
		else
			sed "/^\s*export $variable\>/c\export $variable=$value" $profile > $new_profile
			if [ $? -eq 0 ]
			then
				cp -f $profile $old_profile
				cp -f $new_profile $profile
			else
				echo_tee "set $variable=$value failed--sed"
				return 1
			fi
		fi
	else
		echo "export $variable=$value" >> $profile
	fi

	echo_tee "set $variable=$value ok"
}

################################################
#
#	set env_variable:like variable:PATH etc. 

set_env_like_path()
{
	local variable=$1
	local value=$2

	grep -E "^\s*export $variable\>" $profile
	if [ $? -eq 0 ]
	then
		grep -E "^\s*[^#]*\<$variable=.*:($value:+|$value$)" $profile
		if [ $? -eq 0 ]
		then
			echo_tee "$variable=$value has been set in the $profile"
		else
			local vau=${value//\//\\\/}	
			local key="^\\s*[^#]*\\<$variable=.*"

			#local cmd="sed '1,/$key/s/$key/&:$vau/' $profile > $new_profile"
			#echo_tee "cmd=$cmd"
			#$cmd

			sed '1,/'"$key"'/s/'"$key"'/&:'"$vau"'/' $profile > $new_profile
			if [ $? -eq 0 ]
			then
				cp -f $profile $old_profile
				cp -f $new_profile $profile
			else
				echo_tee "set $variable=$value failed--sed"
				return 1
			fi

		fi
	else
		echo "export $variable=\$$variable:$value" >> $profile
	fi

	echo_tee "set $variable=$value ok"
}

